import os
import json
from unit.models.prefixModel import *
from unit.models.dimensionModel import *
from unit.models.unitModel import *
from unit.models.quantityModel import *
from unit.models.unitSetModel import *
from django.shortcuts import get_object_or_404
from django.db import IntegrityError, transaction



intermediate_data_dir = "./files/unit_related_data/Intermediate_data"
meta_data_dir = "./files/unit_related_data/meta_data"


def to_superscript(number):
    # 将数字转换为字符串
    number_str = str(number)
    # 上标数字映射
    superscript_map = {
        '0': '⁰', '1': '¹', '2': '²', '3': '³', '4': '⁴',
        '5': '⁵', '6': '⁶', '7': '⁷', '8': '⁸', '9': '⁹',
        '.': '⋅', '-': '⁻', '·': '·',
    }
    # 将每个数字字符转换为上标
    superscript_str = ''.join(superscript_map[char] for char in number_str)
    return superscript_str

def genetate_dimension_id_and_basic_unit(data,basic_dimension_qid_basic_dimension_dict):
    dimension_id = ""
    basic_unit = ""
    bsc_dimensions = {}
    if "basic_dimensions" not in data or data["basic_dimensions"] is None or data["basic_dimensions"] == {}:
        dimension_id = "D_Dimensionless"
        basic_unit = "1"
    else:
        for key, value in data['basic_dimensions'].items():
            if value is None or value == "" or value == 0:
                continue
            bsc_dimensions[key] = value
        if len(bsc_dimensions) == 0:
            dimension_id = "D_Dimensionless"
            basic_unit = "1"
        else:
            dimension_id = "D_"
            basic_unit = ""
            bsc_dimensions = dict(sorted(bsc_dimensions.items()))
            for key, value in bsc_dimensions.items():
                bsc_dimension = basic_dimension_qid_basic_dimension_dict[key]
                if value == 1:
                    dimension_id += bsc_dimension.symbol+"·"
                    basic_unit += bsc_dimension.basic_unit+"·"
                else:
                    dimension_id += bsc_dimension.symbol + \
                        to_superscript(value) + "·"
                    basic_unit += bsc_dimension.basic_unit + \
                        to_superscript(value) + "·"
            dimension_id = dimension_id[:-1]
            basic_unit = basic_unit[:-1]
    return True, dimension_id, basic_unit, bsc_dimensions



# 初始化单位前缀数据
def init_prefix():
    meta_prefix_file = os.path.join(meta_data_dir, "unit_prefix_data.json")
    with open(meta_prefix_file, 'r', encoding="utf-8") as f:
        prefix_datas = json.load(f)
    prefix_unit_list = []
    prefix_qid_prefix_dict = {}
    prefix_qid_prefix_uuid_dict = {}
    for prefix_data in prefix_datas:
        user_name = "admin"
        prefix_item = {}
        prefix_item["prefix_name"] = prefix_data["prefix_name"]
        prefix_item["prefix_name_en"] = prefix_data["prefix_name"]
        prefix_item["base"] = prefix_data["base"]
        prefix_item["power"] = prefix_data["power"]
        prefix_item["prefix_id"] = "P"+"_" + \
            str(prefix_data["base"])+"_"+str(prefix_data["power"])
        prefix_item["symbol"] = prefix_data["symbol"]
        prefix_item["symbol_ascii"] = prefix_data["symbol_ascii"]
        prefix_item["symbol_html"] = prefix_data["symbol_html"]
        prefix_item["symbol_latex"] = prefix_data["symbol_latex"]
        prefix_item["symbol_unicode"] = prefix_data["symbol_unicode"]
        prefix_item["comment"] = "系统初始化"
        prefix_item["create_person"] = user_name
        prefix_item["modify_person"] = user_name
        prefix = UnitPrefix(**prefix_item)
        prefix_unit_list.append(prefix)
        prefix_qid_prefix_dict[prefix_data["qid"]] = prefix
    unit_prefix_list = UnitPrefix.objects.bulk_create(prefix_unit_list)
    for qid, prefix in prefix_qid_prefix_dict.items():
        prefix_qid_prefix_uuid_dict[qid] = str(prefix.uuid)
    # 保存qid和uuid的对应关系
    prefix_qid_prefix_uuid_dict_file = os.path.join(intermediate_data_dir, "prefix_qid_prefix_uuid_dict.json")
    with open(prefix_qid_prefix_uuid_dict_file, 'w', encoding="utf-8") as f:
        json.dump(prefix_qid_prefix_uuid_dict, f)
    print("unit prefix init success")
    
# 初始化基本量纲
def init_basic_dimension():
    meta_basic_dimension_file = os.path.join(meta_data_dir, "unit_basic_dimension_data.json")
    with open(meta_basic_dimension_file, 'r', encoding="utf-8") as f:
        basic_dimension_datas = json.load(f)
    basic_dimension_list = []
    basic_dimension_qid_basic_dimension_dict = {}
    basic_dimension_qid_basic_dimension_uuid_dict = {}
    for basic_dimension_data in basic_dimension_datas:
        user_name = "admin"
        basic_dimension_item = {}
        basic_dimension_item["dimension_name"] = basic_dimension_data["dimension_name"]
        basic_dimension_item["dimension_name_en"] = basic_dimension_data["name_en"]
        basic_dimension_item["symbol"] = basic_dimension_data["symbol"]
        basic_dimension_item["symbol_ascii"] = basic_dimension_data["symbol_ascii"]
        basic_dimension_item["symbol_html"] = basic_dimension_data["symbol_html"]
        basic_dimension_item["symbol_latex"] = basic_dimension_data["symbol_latex"]
        basic_dimension_item["symbol_unicode"] = basic_dimension_data["symbol_unicode"]
        basic_dimension_item["basic_unit"] = basic_dimension_data["basic_unit"]
        basic_dimension_item["comment"] = "系统初始化"
        basic_dimension_item["create_person"] = user_name
        basic_dimension_item["modify_person"] = user_name
        basic_dimension = BasicDimension(**basic_dimension_item)
        basic_dimension_list.append(basic_dimension)
        basic_dimension_qid_basic_dimension_dict[basic_dimension_data["qid"]] = basic_dimension
    basic_dimension_list = BasicDimension.objects.bulk_create(basic_dimension_list)
    for qid, basic_dimension in basic_dimension_qid_basic_dimension_dict.items():
        basic_dimension_qid_basic_dimension_uuid_dict[qid] = str(basic_dimension.uuid)
    # 保存qid和uuid的对应关系
    basic_dimension_qid_basic_dimension_uuid_dict_file = os.path.join(intermediate_data_dir, "basic_dimension_qid_basic_dimension_uuid_dict.json")
    with open(basic_dimension_qid_basic_dimension_uuid_dict_file, 'w', encoding="utf-8") as f:
        json.dump(basic_dimension_qid_basic_dimension_uuid_dict, f)
    print("basic dimension init success")
    
# 初始化单位量纲
def init_dimension():
    # 获取基本量纲的映射关系
    basic_dimension_qid_basic_dimension_uuid_dict_file = os.path.join(intermediate_data_dir, "basic_dimension_qid_basic_dimension_uuid_dict.json")
    with open(basic_dimension_qid_basic_dimension_uuid_dict_file, 'r', encoding="utf-8") as f:
        basic_dimension_qid_basic_dimension_uuid_dict = json.load(f)
    basic_dimension_qid_basic_dimension_dict = {}
    for qid, uuid in basic_dimension_qid_basic_dimension_uuid_dict.items():
        basic_dimension_qid_basic_dimension_dict[qid] = get_object_or_404(BasicDimension,uuid=basic_dimension_qid_basic_dimension_uuid_dict[qid])
    
    meta_dimension_file = os.path.join(meta_data_dir, "unit_dimensions_data.json")
    with open(meta_dimension_file, 'r', encoding="utf-8") as f:
        dimension_datas = json.load(f)
        
    dimension_list = []
    dimensionmap_list = []
    unit_list = []
    unit_symbol_list = []
    dimension_qid_dimension_dict = {}
    dimension_qid_dimension_uuid_dict = {}
    dimension_qid_basic_unit_dict = {}
    dimension_qid_basic_unit_uuid_dict = {}
    dimension_set = set()
    
    for dimension_data in dimension_datas:
        user_name = "admin"
        # 构造量纲
        dimension_item = {}
        flag, dimension_id, basic_unit, bsc_dimensions = genetate_dimension_id_and_basic_unit(dimension_data,basic_dimension_qid_basic_dimension_dict)
        if dimension_id in dimension_set:
            print("dimension_id重复：",dimension_data["qid"],dimension_id)
        else:
            dimension_set.add(dimension_id)        
        
        dimension_item["dimension_id"] = dimension_id
        dimension_item["basic_unit"] = basic_unit
        dimension_item["comment"] = "系统初始化"
        dimension_item["create_person"] = user_name
        dimension_item["modify_person"] = user_name
        dimension = Dimension(**dimension_item)
        dimension_list.append(dimension)
        dimension_qid_dimension_dict[dimension_data["qid"]] = dimension
        
        # 构造量纲映射
        for key, value in bsc_dimensions.items():
            bsc_dimension = basic_dimension_qid_basic_dimension_dict[key]
            dimensionmap = DimensionMap(basic_dimension = bsc_dimension, dimension = dimension, power = value)
            dimensionmap_list.append(dimensionmap)
        
        # 构造单位
        unit_item = {}
        unit_item["unit_name"] = basic_unit
        unit_item["unit_name_en"] = basic_unit
        unit_item["symbol"] = basic_unit
        unit_item["dimension"] = dimension
        unit_item["is_system"] = True
        unit_item["comment"] = "{} 系统单位".format(dimension_id)
        unit_item["create_person"] = user_name
        unit_item["modify_person"] = user_name
        unit = Unit(**unit_item)
        unit_list.append(unit)
        dimension_qid_basic_unit_dict[dimension_data["qid"]] = unit
        # 构造单位符号
        unit_symbol_item = {}
        unit_symbol_item["symbol"] = basic_unit
        unit_symbol_item["root_unit"] = unit
        unit_symbol_item["description"] = "{} 系统单位".format(dimension_id)
        unit_symbol_item["is_system"] = True
        unit_symbol_item["create_person"] = user_name
        unit_symbol_item["modify_person"] = user_name
        unit_symbol = UnitSymbol(**unit_symbol_item)
        unit_symbol_list.append(unit_symbol)
    
    dimension_list = Dimension.objects.bulk_create(dimension_list)
    dimensionmap_list = DimensionMap.objects.bulk_create(dimensionmap_list)
    unit_list = Unit.objects.bulk_create(unit_list)
    unit_symbol_list = UnitSymbol.objects.bulk_create(unit_symbol_list)
    
    # 存储量纲的qid和uuid的对应关系
    for qid, dimension in dimension_qid_dimension_dict.items():
        dimension_qid_dimension_uuid_dict[qid] = str(dimension.uuid)
    dimension_qid_dimension_uuid_dict_file = os.path.join(intermediate_data_dir, "dimension_qid_dimension_uuid_dict.json")
    with open(dimension_qid_dimension_uuid_dict_file, 'w', encoding="utf-8") as f:
        json.dump(dimension_qid_dimension_uuid_dict, f)
    # 存储量纲的qid和基本单位的对应关系
    for qid, unit in dimension_qid_basic_unit_dict.items():
        dimension_qid_basic_unit_uuid_dict[qid] = {"uuid":str(unit.uuid),"symbol":unit.symbol}
    dimension_qid_basic_unit_uuid_dict_file = os.path.join(intermediate_data_dir, "dimension_qid_basic_unit_uuid_dict.json")
    with open(dimension_qid_basic_unit_uuid_dict_file, 'w', encoding="utf-8") as f:
        json.dump(dimension_qid_basic_unit_uuid_dict, f)
    print("dimension init success")

# 初始化单位
def init_unit():
    # 获取单位量纲的映射关系
    dimension_qid_dimension_uuid_dict_file = os.path.join(intermediate_data_dir, "dimension_qid_dimension_uuid_dict.json")
    with open(dimension_qid_dimension_uuid_dict_file, 'r', encoding="utf-8") as f:
        dimension_qid_dimension_uuid_dict = json.load(f)
    dimension_qid_dimension_dict = {}
    for qid, uuid in dimension_qid_dimension_uuid_dict.items():
        dimension_qid_dimension_dict[qid] = get_object_or_404(Dimension,uuid=uuid)
    
    # 获取单位量纲与其基本单位的映射关系
    dimension_qid_basic_unit_uuid_dict_file = os.path.join(intermediate_data_dir, "dimension_qid_basic_unit_uuid_dict.json")
    with open(dimension_qid_basic_unit_uuid_dict_file, 'r', encoding="utf-8") as f:
        dimension_qid_basic_unit_uuid_dict = json.load(f)
    dimension_qid_basic_unit_dict = {}
    for qid in dimension_qid_basic_unit_uuid_dict:
        dimension_qid_basic_unit_dict[qid] = get_object_or_404(Unit,uuid=dimension_qid_basic_unit_uuid_dict[qid]["uuid"])
    
    units_file = os.path.join(meta_data_dir, "units.json")
    with open(units_file, 'r', encoding="utf-8") as f:
        unit_datas = json.load(f)
    unit_list = []
    unit_symbol_list = []
    unit_conversion_list = []
    unit_qid_unit_dict = {}
    unit_qid_unit_uuid_dict = {}
    for unit_data in unit_datas:
        user_name = "admin"
        dimension_qid = unit_datas[unit_data]["qid"]
        units_to_create = unit_datas[unit_data]["units"]
        basic_unit = dimension_qid_basic_unit_dict[dimension_qid]
        dimension = dimension_qid_dimension_dict[dimension_qid]
        for unit_to_create in units_to_create:
            # 构造单位
            unit_item = {}
            unit_item["unit_name"] = unit_to_create["name"]
            unit_item["unit_name_en"] = unit_to_create["name"]
            unit_item["symbol"] = unit_to_create["symbol"]
            unit_item["dimension"] = dimension
            unit_item["comment"] = "系统初始化"
            unit_item["create_person"] = user_name
            unit_item["modify_person"] = user_name
            unit = Unit(**unit_item)
            unit_list.append(unit)
            unit_qid_unit_dict[unit_to_create["qid"]] = {"NISTID":unit_to_create["NISTID"],"unit":unit}
            # 构造单位符号
            unit_symbol_item = {}
            unit_symbol_item["symbol"] = unit_to_create["symbol"]
            unit_symbol_item["root_unit"] = unit
            unit_symbol_item["comment"] = "系统创建单位符号"
            unit_symbol_item["create_person"] = user_name
            unit_symbol_item["modify_person"] = user_name
            unit_symbol_item["is_system"] = True
            unit_symbol = UnitSymbol(**unit_symbol_item)
            unit_symbol_list.append(unit_symbol)
            # 构建单位转换关系
            unit_conversion_item = {}
            unit_conversion_item["base_unit"] = basic_unit
            unit_conversion_item["target_unit"] = unit
            unit_conversion_item["factor"] = unit_to_create["factor"]
            unit_conversion_item["offset"] = unit_to_create["offset"]
            unit_conversion_item["create_person"] = user_name
            unit_conversion_item["modify_person"] = user_name
            unit_conversion = UnitConversion(**unit_conversion_item)
            unit_conversion_list.append(unit_conversion)
    unit_list = Unit.objects.bulk_create(unit_list)
    unit_symbol_list = UnitSymbol.objects.bulk_create(unit_symbol_list)
    unit_conversion_list = UnitConversion.objects.bulk_create(unit_conversion_list)
    for qid, unit in unit_qid_unit_dict.items():
        unit_qid_unit_uuid_dict[qid] = {"NISTID":unit["NISTID"],"uuid":str(unit["unit"].uuid)}
    unit_qid_unit_uuid_dict_file = os.path.join(intermediate_data_dir, "unit_qid_unit_uuid_dict.json")
    with open(unit_qid_unit_uuid_dict_file, 'w', encoding="utf-8") as f:
        json.dump(unit_qid_unit_uuid_dict, f)
    print("unit init success")

# 初始化量
def init_physical_quantity():
    # 获取单位量纲的映射关系
    dimension_qid_dimension_uuid_dict_file = os.path.join(intermediate_data_dir, "dimension_qid_dimension_uuid_dict.json")
    with open(dimension_qid_dimension_uuid_dict_file, 'r', encoding="utf-8") as f:
        dimension_qid_dimension_uuid_dict = json.load(f)
    dimension_qid_dimension_dict = {}
    for qid, uuid in dimension_qid_dimension_uuid_dict.items():
        dimension_qid_dimension_dict[qid] = get_object_or_404(Dimension,uuid=uuid)
    
    # 获取单位量纲与其基本单位的映射关系
    dimension_qid_basic_unit_uuid_dict_file = os.path.join(intermediate_data_dir, "dimension_qid_basic_unit_uuid_dict.json")
    with open(dimension_qid_basic_unit_uuid_dict_file, 'r', encoding="utf-8") as f:
        dimension_qid_basic_unit_uuid_dict = json.load(f)
    dimension_qid_basic_unit_dict = {}
    for qid in dimension_qid_basic_unit_uuid_dict:
        dimension_qid_basic_unit_dict[qid] = get_object_or_404(Unit,uuid=dimension_qid_basic_unit_uuid_dict[qid]["uuid"])

    # 获取单位的映射关系
    unit_qid_unit_uuid_dict_file = os.path.join(intermediate_data_dir, "unit_qid_unit_uuid_dict.json")
    with open(unit_qid_unit_uuid_dict_file, 'r', encoding="utf-8") as f:
        unit_qid_unit_uuid_dict = json.load(f)
    unit_qid_unit_dict = {}
    for qid in unit_qid_unit_uuid_dict:
        unit_qid_unit_dict[qid] = get_object_or_404(Unit,uuid=unit_qid_unit_uuid_dict[qid]["uuid"])
    
    # 读取量数据
    physical_quantity_file = os.path.join(meta_data_dir, "quantities.json")
    with open(physical_quantity_file, 'r', encoding="utf-8") as f:
        physical_quantity_datas = json.load(f)
    physical_quantity_list = []
    quantity_unit_list = []
    quantity_qid_quantity_dict = {}
    quantity_qid_quantity_uuid_dict = {}
    
    for physical_quantity_data in physical_quantity_datas:
        user_name = "admin"
        quantity_item = {}
        quantity_item["quantity_name"] = physical_quantity_data["name"]
        quantity_item["quantity_name_en"] = physical_quantity_data["name"]
        quantity_item["quantity_type"] = "NORMAL"
        quantity_item["dimension"] = dimension_qid_dimension_dict[physical_quantity_data["dimension_qid"]]
        quantity_item["comment"] = "系统初始化"
        quantity_item["create_person"] = user_name
        quantity_item["modify_person"] = user_name
        quantity = Quantity(**quantity_item)
        physical_quantity_list.append(quantity)
        quantity_qid_quantity_dict[physical_quantity_data["qid"]] = quantity
        # 构造量单位
        # 添加默认的单位
        dimension_basic_unit = dimension_qid_basic_unit_dict[physical_quantity_data["dimension_qid"]]
        quantity_unit_list.append(QuantityUnitMap(quantity=quantity,unit=dimension_basic_unit))
        units = physical_quantity_data["units"]
        for unit1 in units:
            quantity_unit_list.append(QuantityUnitMap(quantity=quantity,unit=unit_qid_unit_dict[unit1]))
        
    physical_quantity_list = Quantity.objects.bulk_create(physical_quantity_list)
    quantity_unit_list = QuantityUnitMap.objects.bulk_create(quantity_unit_list)
    for qid, quantity in quantity_qid_quantity_dict.items():
        quantity_qid_quantity_uuid_dict[qid] = str(quantity.uuid)
    quantity_qid_quantity_uuid_dict_file = os.path.join(intermediate_data_dir, "quantity_qid_quantity_uuid_dict.json")
    with open(quantity_qid_quantity_uuid_dict_file, 'w', encoding="utf-8") as f:
        json.dump(quantity_qid_quantity_uuid_dict, f)
    print("quantity init success")
        
# 构建一个SI单位集
def init_si_unit_set():
    # 获取单位量纲的映射关系
    dimension_qid_dimension_uuid_dict_file = os.path.join(intermediate_data_dir, "dimension_qid_dimension_uuid_dict.json")
    with open(dimension_qid_dimension_uuid_dict_file, 'r', encoding="utf-8") as f:
        dimension_qid_dimension_uuid_dict = json.load(f)
    dimension_qid_dimension_dict = {}
    for qid, uuid in dimension_qid_dimension_uuid_dict.items():
        dimension_qid_dimension_dict[qid] = get_object_or_404(Dimension,uuid=uuid)
    
    # 获取单位量纲与其基本单位的映射关系
    dimension_qid_basic_unit_uuid_dict_file = os.path.join(intermediate_data_dir, "dimension_qid_basic_unit_uuid_dict.json")
    with open(dimension_qid_basic_unit_uuid_dict_file, 'r', encoding="utf-8") as f:
        dimension_qid_basic_unit_uuid_dict = json.load(f)
    dimension_qid_basic_unit_dict = {}
    for qid in dimension_qid_basic_unit_uuid_dict:
        dimension_qid_basic_unit_dict[qid] = get_object_or_404(Unit,uuid=dimension_qid_basic_unit_uuid_dict[qid]["uuid"])

    # 获取单位的映射关系
    unit_qid_unit_uuid_dict_file = os.path.join(intermediate_data_dir, "unit_qid_unit_uuid_dict.json")
    with open(unit_qid_unit_uuid_dict_file, 'r', encoding="utf-8") as f:
        unit_qid_unit_uuid_dict = json.load(f)
    unit_qid_unit_dict = {}
    for qid in unit_qid_unit_uuid_dict:
        unit_qid_unit_dict[qid] = get_object_or_404(Unit,uuid=unit_qid_unit_uuid_dict[qid]["uuid"])
    
    # 获取量的映射关系
    quantity_qid_quantity_uuid_dict_file = os.path.join(intermediate_data_dir, "quantity_qid_quantity_uuid_dict.json")
    with open(quantity_qid_quantity_uuid_dict_file, 'r', encoding="utf-8") as f:
        quantity_qid_quantity_uuid_dict = json.load(f)
    quantity_qid_quantity_dict = {}
    for qid in quantity_qid_quantity_uuid_dict:
        quantity_qid_quantity_dict[qid] = get_object_or_404(Quantity,uuid=quantity_qid_quantity_uuid_dict[qid])

    # 获取量qid到单位量纲的映射关系
    unit_qid_unit_dict_file = os.path.join(meta_data_dir, "quantities.json")
    with open(unit_qid_unit_dict_file, 'r', encoding="utf-8") as f:
        unit_qid_unit_data = json.load(f)
    quantity_qid_dimension_qid_dict = {}
    for unit_qid_unit_data_item in unit_qid_unit_data:
        quantity_qid_dimension_qid_dict[unit_qid_unit_data_item["qid"]] = unit_qid_unit_data_item["dimension_qid"]
    
    

    # 构建一个SI单位集
    unitset_item = {}
    unitset_item["unit_set_name"] = "SI"
    unitset_item["unit_set_name_en"] = "SI"
    unitset_item["comment"] = "系统初始化"
    unitset_item["create_person"] = "admin"
    unitset_item["modify_person"] = "admin"
    unitset_item["is_system"] = True
    unitset = UnitSet(**unitset_item)
    # 构建单位集和单位的关系
    unitset_unit_list = []
    for qid, quantity in quantity_qid_quantity_dict.items():
        dimension_qid = quantity_qid_dimension_qid_dict[qid]
        basic_unit = dimension_qid_basic_unit_dict[dimension_qid]
        basic_unit_symbol = UnitSymbol.objects.filter(root_unit = basic_unit,is_system=True).first()
        unitset_unit_list.append(UnitSetContent(unit_set = unitset,unit_quantity = quantity,unit_symbol = basic_unit_symbol))
    with transaction.atomic():
        unitset.save()
        UnitSetContent.objects.bulk_create(unitset_unit_list)
        
    print("si unit set init success")
    
# 获取一下量纲的基本数据情况
def get_dimension_data_status():
    # 获取单位量纲的映射关系
    dimension_qid_dimension_uuid_dict_file = os.path.join(intermediate_data_dir, "dimension_qid_dimension_uuid_dict.json")
    with open(dimension_qid_dimension_uuid_dict_file, 'r', encoding="utf-8") as f:
        dimension_qid_dimension_uuid_dict = json.load(f)
    dimension_qid_dimension_dict = {}
    for qid, uuid in dimension_qid_dimension_uuid_dict.items():
        dimension_qid_dimension_dict[qid] = get_object_or_404(Dimension,uuid=uuid)
    dimension_data_list = []
    for qid, dimension in dimension_qid_dimension_dict.items():
        dimension_item = {}
        dimension_item["dimension_qid"] = qid
        dimension_item["dimension_id"] = dimension.dimension_id
        dimension_item["basic_unit"] = dimension.basic_unit
        basic_dimension_list = []
        for dimensionmap in dimension.dimensionmap_set.all():
            basic_dimension_item = {}
            basic_dimension_item["basic_dimension_name"] = dimensionmap.basic_dimension.dimension_name
            basic_dimension_item["power"] = dimensionmap.power
            basic_dimension_list.append(basic_dimension_item)
        basic_dimension_string = ""
        for basic_dimension_item in basic_dimension_list:
            basic_dimension_string += basic_dimension_item["basic_dimension_name"] + ":" + str(basic_dimension_item["power"]) + "\n"
        dimension_item["basic_dimensions"] = basic_dimension_string
        dimension_data_list.append(dimension_item)
    
    dimension_data_file = os.path.join(intermediate_data_dir, "dimension_data.json")
    with open(dimension_data_file, 'w', encoding="utf-8") as f:
        json.dump(dimension_data_list, f)        
    


# 这是初始化所有单位数据的调用函数
def init_unit_data():
    if os.path.exists(intermediate_data_dir) == False:
        os.makedirs(intermediate_data_dir)
    init_prefix()
    init_basic_dimension()
    init_dimension()
    init_unit()
    init_physical_quantity()
    init_si_unit_set()
    get_dimension_data_status()
    
    print("init_unit_data")
